<?php

namespace App\Admin\Controllers\Auth;

use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use App\Http\Controllers\Controller;

class MenuController extends Controller
{
    use HasResourceActions;

    protected $title = '管理端菜单';

    protected function model()
    {
        return config('admin.database.menu_model');
    }

    public function index(Content $content)
    {
        return $content
            ->title($this->title)
            ->description(trans('admin.list'))
            ->row(function (Row $row) {
                $row->column(6, $this->treeView()->render());

                $row->column(6, function (Column $column) {
                    $class = $this->model();
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(url(request()->getPathInfo()));

                    $form->select('parent_id', trans('admin.parent_id'))->options($class::selectOptions());
                    $form->text('title', trans('admin.title'))->rules('required');
                    $form->icon('icon', trans('admin.icon'))->default('fa-bars')->rules('required')->help($this->iconHelp());
                    $form->text('uri', trans('admin.uri'));
                    $this->extendForm($form);
                    $form->hidden('_token')->default(csrf_token());
                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });
    }

    /**
     * @return \Encore\Admin\Tree
     */
    protected function treeView()
    {
        $class = $this->model();
        $tree = new Tree(new $class());

        $tree->disableCreate();

        $tree->branch(function ($branch) {
            $is_show = data_get($branch, 'is_show', 1);
            $payload = sprintf('<i class="fa %s"></i>&nbsp;<strong style="%s">%s</strong>',
                $branch['icon'],
                data_get(['text-decoration: line-through;color:red'], $is_show, ''),
                $branch['title']
            );

            if (!isset($branch['children'])) {
                $uri = $branch['uri'];
                $payload .= "&nbsp;&nbsp;&nbsp;<a class=\"dd-nodrag\">$uri</a>";
            }

            return $payload;
        });

        return $tree;
    }

    /**
     * Edit interface.
     *
     * @param string $id
     * @param Content $content
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->title(trans('admin.menu'))
            ->description(trans('admin.edit'))
            ->row($this->form()->edit($id));
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $class = $this->model();
        $form = new Form(new $class());

        $form->select('parent_id', trans('admin.parent_id'))->options($class::selectOptions());
        $form->text('title', trans('admin.title'))->rules('required');
        $form->icon('icon', trans('admin.icon'))->default('fa-bars')->rules('required')
            ->help('For more icons please see');
        $form->text('uri', trans('admin.uri'));
        $states = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('is_show', '是否显示')->states($states);
        $this->extendForm($form);
        return $form;
    }

    protected function extendForm($form)
    {

    }

    /**
     * Help message for icon field.
     *
     * @return string
     */
    protected function iconHelp()
    {
        return 'For more icons please see';
    }

}
